package org.phalanx.gwt.client.graphics.units;

import java.util.ArrayList;
import java.util.HashMap;

import org.phalanx.gwt.client.graphics.map.MapCoordinates;
import org.phalanx.gwt.client.graphics.map.PhalanxMap;

import com.google.gwt.user.client.Window;

public class PhalanxMapUnitMatrix {
	private HashMap<MapCoordinates,PhalanxMapUnit> gameUnitTable;
	private PhalanxMapUnitSet mapUnitSet;
	private PhalanxMap pMap;
	
	public PhalanxMapUnitMatrix(PhalanxMap pMap, PhalanxMapUnitSet uSet){
		gameUnitTable = new HashMap<MapCoordinates, PhalanxMapUnit>();
		setMap(pMap);
		setMapUnitSet(uSet);
	}

	public void setMap(PhalanxMap pMap) {
		this.pMap = pMap;
	}

	public PhalanxMap getMap() {
		return pMap;
	}

	public void setMapUnitSet(PhalanxMapUnitSet mapUnitSet) {
		this.mapUnitSet = mapUnitSet;
	}

	public PhalanxMapUnitSet getMapUnitSet() {
		return mapUnitSet;
	}
	
	public void addUnitAt(MapCoordinates c, PhalanxMapUnit unit){
		
		if (getMapUnitSet().hasUnit(unit)){
			if (getIsInBounds(c)){
				gameUnitTable.put(c, unit);
			}
		}
	}
	
	public void removeUnitAt(MapCoordinates c){
		if (gameUnitTable.containsKey(c)){
			gameUnitTable.remove(c);
		}
	}
	
	public void clearUnits(){
		gameUnitTable.clear();
	}
	
	public PhalanxMapUnit getUnitAt(MapCoordinates c){
		PhalanxMapUnit unit = null;
		
		if (gameUnitTable.containsKey(c)){
			unit = gameUnitTable.get(c);
		}
		
		return unit;
	}
	
	private boolean getIsInBounds(MapCoordinates c){
		boolean isBounds = false;
		
		if (c.getX() >= 0 && c.getY() >= 0 && c.getX() < pMap.getMapWidth() && c.getY() < pMap.getMapLength()){
			isBounds = true;
		}
		
		return isBounds;
	}
	
	public static void getMatrixFromSerializedData(PhalanxMapUnitMatrix pMat, String serializedData){
		String[] unitData = serializedData.split(":");
		pMat.clearUnits();
		
		if (unitData.length > 0){
			for (int i = 0; i < unitData.length; i++){
				String oneUnit = unitData[i];
				oneUnit = oneUnit.substring(1, oneUnit.length() - 1);
				
				String[] unitProperties = oneUnit.split("!");
				
				if (unitProperties.length == 6){
//					Window.alert(unitProperties[0]);
					PhalanxMapUnit tempUnit = new PhalanxMapUnit(unitProperties[0], unitProperties[0]);
					tempUnit.setUnitId(Integer.valueOf(unitProperties[1]));
					tempUnit.setUserId(Integer.valueOf(unitProperties[2]));
					MapCoordinates c = new MapCoordinates(Integer.valueOf(unitProperties[3]), Integer.valueOf(unitProperties[4]));
					tempUnit.setHitPoints(Integer.valueOf(unitProperties[5]));
					
					pMat.addUnitAt(c, tempUnit);
				}
			}
		}
	}
	
	public static ArrayList<MapCoordinates> getFOVfromSerializedData(String serializedData){
		ArrayList<MapCoordinates> visibilityArray = new ArrayList<MapCoordinates>();
		String[] coordinatesArray = serializedData.split(":");
		
		if (coordinatesArray.length > 0){
			for (int i = 0; i < coordinatesArray.length; i++){
				String oneCoor = coordinatesArray[i];
				oneCoor = oneCoor.substring(1, oneCoor.length() - 1);
				
				String[] xyCoor = oneCoor.split("!");
				
				if (xyCoor.length > 0){
					MapCoordinates c = new MapCoordinates(Integer.valueOf(xyCoor[0]), Integer.valueOf(xyCoor[1]));
					
					visibilityArray.add(c);
				}
			}
		}
		return visibilityArray;
	}
}
